package com.cocoiko.tools.twitter;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import oauth.signpost.OAuth;
import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.cocoiko.info.FriendInfo;
import com.cocoiko.utils.CocoikoAccount;
import com.cocoiko.utils.CocoikoConstains;
import com.cocoiko.utils.CocoikoUtils;

public class TwitterUtils {

	public static boolean isAuthenticated(SharedPreferences prefs) {

		String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
		String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

		AccessToken a = new AccessToken(token, secret);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(CocoikoConstains.CONSUMER_KEY,
				CocoikoConstains.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);
		try {
			twitter.getAccountSettings();
			return true;
		} catch (TwitterException e) {
			return false;
		}
	}

	public static boolean isAuthenticated(Twitter twitter) {

		try {
			twitter.getAccountSettings();
			return true;
		} catch (TwitterException e) {
			return false;
		}
	}

	public static Twitter createTwitterInstant(SharedPreferences prefs) {

		String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
		String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

		AccessToken a = new AccessToken(token, secret);
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(CocoikoConstains.CONSUMER_KEY,
				CocoikoConstains.CONSUMER_SECRET);
		twitter.setOAuthAccessToken(a);
		if (isAuthenticated(twitter))
			return twitter;
		else
			return null;
	}

	/*
	 * get friend list
	 */

	public static ArrayList<FriendInfo> getFriendsList(SharedPreferences prefs) {
		ArrayList<FriendInfo> list = new ArrayList<FriendInfo>();

		Twitter twitter = createTwitterInstant(prefs);
		try {
			IDs ids = twitter.getFriendsIDs(-1);
			long id[] = ids.getIDs();
			for (int index = 0; index < id.length; index++) {
				FriendInfo f = new FriendInfo();

				User user = twitter.showUser(id[index]);

				f.setId(user.getId());
				f.setName(user.getScreenName());
				f.setKokoikoAccount(true);
				URL url = user.getProfileImageURL();
				Bitmap bitmap = CocoikoUtils.getBitmapFromUrl(url);
				f.setAvatar(bitmap);

				list.add(f);
			}
			int friend_count = twitter.getAccountTotals().getFriends();
			int follower_count = twitter.getAccountTotals().getFollowers();
			Log.i("friend", String.valueOf(friend_count));
			Log.i("follow", String.valueOf(follower_count));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	/**
	 * 
	 * @param prefs
	 * @return
	 */
	public static CocoikoAccount getTwitterUserProfile(SharedPreferences prefs) {

		CocoikoAccount account = new CocoikoAccount();
		Twitter twitter = createTwitterInstant(prefs);
		if (twitter != null) {
			long id;
			try {
				id = twitter.getId();
				User user = twitter.showUser(id);

				account.setId(String.valueOf(id));
				account.setName(user.getScreenName());
				account.setAvatar(CocoikoUtils.getBitmapFromUrl(user
						.getProfileImageURL()));
				
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return account;
	}

}
